var Scene = function (game) {
    var s = {
        game: game,
    }
    // 初始化
    var paddle = Paddle(game)
    var ball = Ball(game)
    game.registerAction('a', function () {
        // log('paddle', paddle)
        paddle.moveLeft()
    })
    game.registerAction('d', function () {
        paddle.moveRight()
    })
    // 控制小球开始运动
    ball.fire()
    window.scores = 0;

    // 选择关卡
    s.loadlevel = function (n) {
        // console.log('load level')
        n -= 1
        let blocks = []
        let level = levels[n]
        for (let i = 0; i < level.length; i++) {
            let b = Block(level[i],game)
            blocks.push(b)
        }
        // console.log(blocks, 'blocks')
        return blocks
    }
    
    // 为了debugMode blocks 定义到 windows 上
    window.blocks = s.loadlevel(1)
    
    s.draw = function() {
        game.drawImage(paddle)
        game.drawImage(ball)
        for (let i = 0; i < blocks.length; i++) {
            const b = blocks[i]
            if (b.alive) {
                game.drawImage(b)
            }
        }
        game.ctx.fillText('当前分数' + ': ' + scores, 10, 290);
        
    }
    s.update = function() {
        if (paused) {
            return
        }
        ball.move()
        // 游戏结束
        if (ball.y > paddle.y) {
            var end = SceneEnd(game)
            game.replaceScene(end)
            titleMessage.fire({ 'type': 'MESSAGE', 'message': 'GameOver' })
            return
        }
        // 判断碰撞
        if (paddle.collide(ball)) {
            ball.rebound(paddle)
        }
        for (let i = 0; i < blocks.length; i++) {
            let b = blocks[i]
            if (b.collide(ball)) {
                b.kill()
                ball.rebound(b)
            }
        }
    }
    enableDebugMode(ball, true, s)
    return s

}